import { showToast, showLoadingToast } from "vant";
import request from '@/api/index';
// vite中简写动态图片路径
export const getAssetsFile = (url: string,name='' as string) => {
    const fileName = name?name:import.meta.env.VITE_THEME
    return new URL(`../assets/${fileName}/${url}`, import.meta.url).href
}
// oss 图片压缩
export const getOSSFile = (url: string,name='' as string) => {
    return `${url}?x-oss-process=image/resize,w_120/quality,q_50`
}
// 用户名称6字省略
export const nameFilter = (val:any ='',num:number = 6,sign:number=3) => {
    let arr = val.split('...')
    if(arr[arr.length - 1] == '') return val
    if(val.length > num) {
        let signtext = ''
        for(let i=1;i<=sign;i++) {
            signtext+='.'
        }
        val = val.slice(0,num) + signtext
    }
    return val
};
export const ToUserAndRoom = (item:any = {})=>{
    const { user_id=0, room_id=0, room_open=0 } = item
    if(user_id) {
        return (kk as any).goCommon({ code: "100001", user_id });
    }
    if(room_id && room_open == 1) {
        let data = {
            code: 100002,
            room_id,
        };
        return (kk as any).goCommon(data);
    }
}
export const tpreventoftenFun = (fn:Function,delay=1000) => {
    let timer = 0
    return function (...args:any) {
        if(timer) return
        fn.apply(fn,args)
        timer = setTimeout(()=>{
            timer && clearTimeout(timer)
            timer = 0
        },delay)
    }
}

// vant提示框
export const xmtoast = (message:any = '') => {
    showToast({
      message,
      className:'xmToast',
    })
}
// vant加载框
export const xmloading = (message = '加载中...') => {
    const loading = showLoadingToast({
        message,
        forbidClick: true,
        loadingType:'spinner',
        duration:0,
        className:'xmloading',
    })
    return loading
}
export const iosOrAndroid = () => {
    let ios = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    let Android = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1;
    if (ios) {
        return 'ios'
    } else if (Android) {
        return 'Android'
    }
}
// 截取url后面字段
export const getUrlParams = function (key: any) {
    let url = window.location.href;
    const arr = url.split("?");
    const newArr = arr[1] ? arr[1].split("&") : [];
    for (var i = 0; i < newArr.length; i++) {
        let temp = newArr[i].split("=");
        if (temp[0] === key) {
            let search = temp[1];
            if (search.indexOf("#") > 0) {
                search = search.substring(0, search.indexOf("#"));
            }
            return search;
        }
    }
}